package manager;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import myframe.MyFrame;

public class Login extends JFrame {

    private JPanel contentPane; // 主面板
	private JTextField username; // 用于输入用户名
	private JTextField password; // 用于输入密码
	private JLabel title; // 显示窗口的title
	private JLabel minisize; // 最小化按钮
	private JLabel close; // 关闭按钮
	private JLabel appName; // 系统名字
	private JLabel bg_view; // 界面的背景
	private JCheckBox showpwd; // 显示密码复选框
	private JCheckBox rempwd; // 记住密码复选框
	private JButton login; // 登录按钮
	private JPanel bg_title; // 窗口上边的背景色

	private int mX, mY,eX,eY;	//用于实现窗口拖拽的变量

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());
//					UIManager.setLookAndFeel(new NimbusLookAndFeel());
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 341, 259);
		setLocationRelativeTo(null);
		Image image = Toolkit.getDefaultToolkit().getImage(
				getClass().getResource("/img/icon.jpg"));
		setIconImage(image);
		setResizable(false);
		setUndecorated(true);

		initView();
		initEvent();
	}

	private void initView() {
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new LineBorder(new Color(30, 144, 255)));
		setContentPane(contentPane);

		bg_title = new JPanel();
		bg_title.setBounds(0, 0, 341, 30);
		bg_title.setBackground(new Color(204, 204, 255));

		JLabel label_2 = new JLabel("\u7528\u6237\u540D\uFF1A");
		label_2.setBounds(53, 128, 48, 15);

		username = new JTextField();
		username.setFont(new Font("宋体", Font.PLAIN, 14));
		username.setBounds(105, 125, 165, 21);
		username.setColumns(10);

		title = new JLabel("\u767B\u5F55");
		title.setBounds(10, 6, 26, 15);
		title.setFont(new Font("宋体", Font.BOLD, 12));
		title.setHorizontalAlignment(SwingConstants.CENTER);

		close = new JLabel("\u00D7");
		close.setBounds(312, 1, 29, 24);
		close.setFont(new Font("宋体", Font.BOLD, 13));
		close.setHorizontalAlignment(SwingConstants.CENTER);

		minisize = new JLabel("\u2014");
		minisize.setFont(new Font("宋体", Font.BOLD, 13));
		minisize.setBounds(280, 1, 29, 24);
		minisize.setHorizontalAlignment(SwingConstants.CENTER);

		bg_title.setLayout(null);
		bg_title.add(title);
		bg_title.add(close);
		bg_title.add(minisize);

		contentPane.setLayout(null);
		contentPane.add(label_2);
		contentPane.add(username);
		contentPane.add(bg_title);

		JLabel label_3 = new JLabel("\u5BC6  \u7801\uFF1A");
		label_3.setBounds(53, 164, 54, 15);
		contentPane.add(label_3);

		password = new JTextField();
		password.setFont(new Font("宋体", Font.PLAIN, 14));
		password.setBounds(105, 158, 165, 21);
		contentPane.add(password);
		password.setColumns(10);

		rempwd = new JCheckBox("\u8BB0\u4F4F\u5BC6\u7801");
		rempwd.setBounds(188, 186, 82, 23);
		contentPane.add(rempwd);

		login = new JButton("\u767B  \u5F55");
		login.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		login.setBounds(131, 219, 93, 30);
		contentPane.add(login);

		showpwd = new JCheckBox("\u663E\u793A\u5BC6\u7801");
		showpwd.setBounds(105, 185, 81, 23);
		contentPane.add(showpwd);

		appName = new JLabel("\u81EA\u52A8\u704C\u6E89\u7CFB\u7EDF");
		appName.setFont(new Font("楷体", Font.BOLD, 18));
		appName.setForeground(Color.WHITE);
		appName.setBounds(21, 41, 121, 30);
		contentPane.add(appName);

		bg_view = new JLabel("");
		bg_view.setIcon(new ImageIcon(Login.class
				.getResource("/img/bg_login.jpg")));
		bg_view.setBounds(0, 30, 341, 77);
		contentPane.add(bg_view);

	}

	private void initEvent() {
		//实现窗口拖拽
		bg_title.addMouseMotionListener(new MouseMotionAdapter() {
			@Override
			public void mouseDragged(MouseEvent e) {
				int nX = e.getLocationOnScreen().x;
				int nY = e.getLocationOnScreen().y;
				setLocation(mX+nX-eX, mY+nY-eY);
			}
		});
		bg_title.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				eX = e.getLocationOnScreen().x;
				eY = e.getLocationOnScreen().y;
				mX = getLocationOnScreen().x;
				mY = getLocationOnScreen().y;
			}
		});

		//窗口关闭
		close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				close.setOpaque(true);
				close.setBackground(Color.RED);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				close.setBackground(bg_title.getBackground());
			}
		});

		//窗口最小化
		minisize.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				setExtendedState(ICONIFIED);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				minisize.setOpaque(true);
				minisize.setBackground(Color.lightGray);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				minisize.setBackground(bg_title.getBackground());
			}
		});

		//登录
		login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new MyFrame();
				dispose();
			}
		});
	}

}